from pydantic import BaseModel, EmailStr
from typing import List, Optional
from datetime import datetime

# 用户相关模型
class UserBase(BaseModel):
    username: str
    is_active: bool = True
    is_admin: bool = False
    points: int = 0

class UserCreate(UserBase):
    password: str
    email: EmailStr

class UserUpdate(BaseModel):
    username: Optional[str] = None
    password: Optional[str] = None
    is_active: Optional[bool] = None
    is_admin: Optional[bool] = None
    points: Optional[int] = None
    email: Optional[EmailStr] = None

class User(UserBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        orm_mode = True

# 邮箱相关模型
class EmailBase(BaseModel):
    email: EmailStr
    is_primary: bool = False

class EmailCreate(EmailBase):
    user_id: int

class Email(EmailBase):
    id: int
    user_id: int
    created_at: datetime

    class Config:
        orm_mode = True

# 订阅相关模型
class SubscriptionBase(BaseModel):
    name: str
    description: str
    email_subject: str  # 新增邮件主题字段
    message_template: str
    message_params: str
    points_cost: int = 0

class SubscriptionCreate(SubscriptionBase):
    pass

class SubscriptionUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    email_subject: Optional[str] = None  # 新增邮件主题字段
    message_template: Optional[str] = None
    message_params: Optional[str] = None
    points_cost: Optional[int] = None

class Subscription(SubscriptionBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        orm_mode = True

# 用户订阅相关模型
class UserSubscriptionBase(BaseModel):
    user_id: int
    subscription_id: int

class UserSubscriptionCreate(UserSubscriptionBase):
    pass

class UserSubscription(UserSubscriptionBase):
    id: int
    created_at: datetime

    class Config:
        orm_mode = True

# 积分日志相关模型
class PointLogBase(BaseModel):
    user_id: int
    points_change: int
    reason: str

class PointLogCreate(PointLogBase):
    pass

class PointLog(PointLogBase):
    id: int
    created_at: datetime

    class Config:
        orm_mode = True

# 管理员邮箱相关模型
class AdminEmailBase(BaseModel):
    email: EmailStr
    smtp_server: str
    smtp_port: int
    username: str
    password: str
    is_default: bool = False

class AdminEmailCreate(AdminEmailBase):
    pass

class AdminEmailUpdate(BaseModel):
    email: Optional[EmailStr] = None
    smtp_server: Optional[str] = None
    smtp_port: Optional[int] = None
    username: Optional[str] = None
    password: Optional[str] = None
    is_default: Optional[bool] = None

class AdminEmail(AdminEmailBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        orm_mode = True

# 邮件日志相关模型
class EmailLogBase(BaseModel):
    from_email: EmailStr
    to_email: str
    subject: str
    content: str
    status: str
    error_message: Optional[str] = None

class EmailLogCreate(EmailLogBase):
    pass

class EmailLog(EmailLogBase):
    id: int
    created_at: datetime

    class Config:
        orm_mode = True

# API日志相关模型
class APILogBase(BaseModel):
    endpoint: str
    method: str
    params: str
    status_code: int
    response_time: float
    user_id: Optional[int] = None
    error_message: Optional[str] = None

class APILogCreate(APILogBase):
    pass

class APILog(APILogBase):
    id: int
    created_at: datetime

    class Config:
        orm_mode = True


class PointsCreate:
    pass


class UserGroupCreate(BaseModel):
    name: str
    description: Optional[str] = None